namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;

    public class IndicatorRateOfChange : BaseIndicator
    {
        private LineShape tlsRoc;
        private LineShape tlsRocMA;

        public IndicatorRateOfChange()
        {
            base.IndType = IndicatorType.ROC;
            base.IndName = "ROC";
            base.IndDesc = "变动速率指标";
            base.IndPeriod.Add(new IndicatorPeriod(0, "N", 12.0));
            base.IndPeriod.Add(new IndicatorPeriod(1, "M", 6.0));
            base.SourceDic["Close"] = "";
            base.fields = new string[] { "ROC", "ROCMA" };
        }

        public override bool Contains(object obj)
        {
            if ((obj != this.tlsRoc) && (obj != this.tlsRocMA))
            {
                return false;
            }
            return true;
        }

        public override double[] Get(int r)
        {
            double curValue = 0.0;
            int num2 = r - ((int) base.IndPeriod[0].ParamValue);
            if (num2 < 0)
            {
                num2 = 0;
            }
            double num3 = Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["Close"]]);
            double num4 = Convert.ToDouble(base.Crab.DataSource.Rows[num2][base.SourceDic["Close"]]);
            if (num4 != 0.0)
            {
                curValue = (100.0 * (num3 - num4)) / num4;
            }
            double num5 = Common.CalcuteMovingAvg(r, (int) base.IndPeriod[1].ParamValue, this.tlsRocMA.Name, this.tlsRoc.Name, curValue, base.Crab.DataSource);
            return new double[] { curValue, num5 };
        }

        public override List<BaseShape> GetShapeList()
        {
            return new List<BaseShape> { this.TlsRoc, this.tlsRocMA };
        }

        public override void Remove()
        {
            base.Crab.DeleteShape(this.TlsRoc);
            base.Crab.DeleteShape(this.TlsRocMA);
        }

        public override bool Set(int r, string target)
        {
            try
            {
                DataRow row = base.Crab.DataSource.Rows[r];
                foreach (string str in base.SourceDic.Values)
                {
                    if ((str == target) || (target == null))
                    {
                        foreach (string str2 in base.SourceDic.Values)
                        {
                            if (row[str2] is DBNull)
                            {
                                return false;
                            }
                        }
                        double[] numArray = this.Get(r);
                        base.Crab.SetValue(this.TlsRoc.Name, numArray[0], r);
                        base.Crab.SetValue(this.TlsRocMA.Name, numArray[1], r);
                        return true;
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        public override void SetParam(string[] fields, string[] source, double[] periods, int panelID)
        {
            this.TlsRoc = base.Crab.AddLine(fields[0], panelID);
            base.Crab.SetLine(fields[0], fields[0], Color.White, 1, 0.01, 2, DashStyle.Solid);
            this.TlsRocMA = base.Crab.AddLine(fields[1], panelID);
            base.Crab.SetLine(fields[1], fields[1], Color.Yellow, 1, 0.01, 2, DashStyle.Solid);
            base.SourceDic["Close"] = source[0];
            base.IndPeriod[0].ParamValue = periods[0];
            base.IndPeriod[0].ParamValue = periods[1];
        }

        public LineShape TlsRoc
        {
            get
            {
                return this.tlsRoc;
            }
            set
            {
                this.tlsRoc = value;
            }
        }

        public LineShape TlsRocMA
        {
            get
            {
                return this.tlsRocMA;
            }
            set
            {
                this.tlsRocMA = value;
            }
        }
    }
}

